//
//  WYSendPacketHeaderCell.m
//  Boss
//
//  Created by admin on 2018/6/5.
//  Copyright © 2018年 admin. All rights reserved.
//

#import "WYSendPacketHeaderCell.h"
#import "WYAddImageViewCell.h"
#import <KSPhotoBrowser/KSPhotoBrowser.h>
@interface WYSendPacketHeaderCell()<UICollectionViewDelegate,UICollectionViewDataSource,KSPhotoBrowserDelegate,UITextViewDelegate>

@property (nonatomic,strong)UICollectionView *collectionView;
@property (nonatomic,strong)NSMutableArray *dataArray;

@end
@implementation WYSendPacketHeaderCell

-(void)setImageArray:(NSMutableArray *)ImageArray
{
    _ImageArray = ImageArray;
    self.dataArray = ImageArray;
    [self reloadCell];
    
    
}




-(NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [[NSMutableArray alloc]init];
    }
    return _dataArray;
}
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    
    if (self =[super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self createSubViews];
    }
    return self;
}

-(void)createSubViews
{
    _inputView = [[UITextView alloc]initWithFrame:CGRectMake(15, 95, kScreenWidth-30, 100)];
    _inputView.font = [UIFont systemFontOfSize:16 ];
    
    
    // _inputView.cornerRadius = 0;
//    _inputView.placeholderColor = [UIColor lightGrayColor];
    _inputView.scrollEnabled = YES;
    //_inputView.placeholderFont = [UIFont systemFontOfSize:22];
    // 设置文本框最大行数
    WEAKSELF
//    [_inputView textValueDidChanged:^(NSString *text, CGFloat textHeight) {
//        //        CGRect frame = _inputView.frame;
//        //        frame.size.height = textHeight;
//        //        _inputView.frame = frame;
//        if (textHeight>180) {
////            [weakSelf.inputView mas_remakeConstraints:^(MASConstraintMaker *make) {
////                make.top.mas_equalTo(0);
////                make.left.mas_equalTo(15);
////                make.right.mas_equalTo(-15);
////                make.height.mas_equalTo(textHeight);
////            }];
//            [weakSelf.inputView mas_updateConstraints:^(MASConstraintMaker *make) {
//                make.height.mas_equalTo(textHeight);
//            }];
//        }
//    }];
//    _inputView.placeholder = @"请输入描述内容";
   //_inputView.maxNumberOfLines = 400;
    [self addSubview:_inputView];
    [_inputView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(8);
        make.left.mas_equalTo(16);
        make.right.mas_equalTo(-16);
        make.height.mas_equalTo(180);
    }];
    _inputView.delegate = self;
    [self setupTextView];
    
    UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc]init];
    layout.itemSize=CGSizeMake((kScreenWidth-40-10-10)/3, (kScreenWidth-40-10-10)/3);
    //横向最小距离
    layout.minimumInteritemSpacing=10.0f;
    layout.minimumLineSpacing=10.0f;
    //cell距离上  左  下 右 的距离
    layout.sectionInset=UIEdgeInsetsMake(10, 10, 10, 10);
    [layout setScrollDirection:UICollectionViewScrollDirectionVertical];
    UICollectionView * collectionView= [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, 0, 0) collectionViewLayout:layout];
    // [collectionView setCollectionViewLayout:layout];
    [self addSubview:collectionView];
    
    [collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(10);
        make.right.mas_equalTo(-10);
        make.bottom.mas_equalTo(0);
        make.top.equalTo(weakSelf.inputView.mas_bottom).offset(5);
        make.height.mas_equalTo((kScreenWidth-40-10-10)/3+20);
        
    }];
    self.collectionView = collectionView;
    self.collectionView.bounces = NO;
    [collectionView registerNib:[UINib nibWithNibName:@"WYAddImageViewCell" bundle:nil] forCellWithReuseIdentifier:@"WYAddImageViewCell"];
    collectionView.delegate = self;
    collectionView.dataSource = self;
    collectionView.backgroundColor = [UIColor whiteColor];
    
    
    
    
    
}
- (void)setupTextView
{
    
    UILabel *placeHolderLabel = [[UILabel alloc] init];
    placeHolderLabel.text = @"hi，一起分享你的故事...";
    placeHolderLabel.numberOfLines = 0;
    placeHolderLabel.textColor = [UIColor lightGrayColor];
    [placeHolderLabel sizeToFit];
    [self.inputView addSubview:placeHolderLabel];
    placeHolderLabel.font = [UIFont systemFontOfSize:18.f];
    [self.inputView setValue:placeHolderLabel forKey:@"_placeholderLabel"];
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (self.dataArray.count>=9) {
        return 9;
    }else{
        return self.dataArray.count+1;
    }
    
    
    
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    WYAddImageViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"WYAddImageViewCell" forIndexPath:indexPath];
    if (!cell) {
        cell = [[NSBundle mainBundle]loadNibNamed:@"WYAddImageViewCell" owner:nil options:nil].firstObject;
    }
    if (self.dataArray.count>=9) {
        cell.deleteBtn.hidden = NO;
        if ([self.dataArray[indexPath.row] isKindOfClass:[UIImage class]]) {
            cell.addImageView.image =self.dataArray[indexPath.row];
        }
        if ([self.dataArray[indexPath.row] isKindOfClass:[NSString class]]) {
            [cell.addImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@!default",self.dataArray[indexPath.row]]] placeholderImage:[UIImage imageNamed:@"logo_place"]];
        }
    }else{
        if (indexPath.row == self.dataArray.count) {
            cell.deleteBtn.hidden = YES;
            cell.addImageView.image =[UIImage imageNamed:@"image_add_icon"];
        }else{
            cell.deleteBtn.hidden = NO;
            if ([self.dataArray[indexPath.row] isKindOfClass:[UIImage class]]) {
                cell.addImageView.image =self.dataArray[indexPath.row];
            }
            if ([self.dataArray[indexPath.row] isKindOfClass:[NSString class]]) {
                [cell.addImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@!default",self.dataArray[indexPath.row]]] placeholderImage:[UIImage imageNamed:@"logo_place"]];
            }
            
            
            
        }
        
    }
    
    
//    if (indexPath.row==self.dataArray.count-1) {
//
//    }else{
//
//    }
//
    cell.deleteBtn.tag = indexPath.row;
    [cell.deleteBtn addTarget:self action:@selector(deleteBtnClik:) forControlEvents:UIControlEventTouchUpInside];
    return cell;
}
- (void)deleteBtnClik:(UIButton *)sender {    
    if ([self.delegate respondsToSelector:@selector(deleteImageClick:)]) {
        [self.delegate deleteImageClick:sender.tag];
    }
}





-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == self.dataArray.count) {
        WYAddImageViewCell *cell = (WYAddImageViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
//        [self.dataArray insertObject:@"example" atIndex:self.dataArray.count - 1];
        if ([self.delegate respondsToSelector:@selector(didSelectCollectionViewWithIndex:WithCell:)]) {
            [self.delegate didSelectCollectionViewWithIndex:indexPath WithCell:cell];
        }
        
        
    }else{
        
        
        
//        NSMutableArray *items = @[].mutableCopy;
//        for (int i = 0; i < self.dataArray.count; i++) {
//            NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
//            WYAddImageViewCell *cell =( WYAddImageViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
//            UIImageView *imgV = cell.addImageView;
//            UIImage *image =self.dataArray[i];
//            KSPhotoItem *item = [KSPhotoItem itemWithSourceView:imgV image:image];
//            [items addObject:item];
//        }
//        KSPhotoBrowser *browser = [KSPhotoBrowser browserWithPhotoItems:items selectedIndex:indexPath.row];
//        browser.delegate = self;
//        browser.dismissalStyle = KSPhotoBrowserInteractiveDismissalStyleRotation;
//        browser.backgroundStyle = KSPhotoBrowserBackgroundStyleBlurPhoto;
//        browser.loadingStyle = KSPhotoBrowserImageLoadingStyleDeterminate;
//        browser.pageindicatorStyle = KSPhotoBrowserPageIndicatorStyleDot;
//        browser.bounces = NO;
//        [browser showFromViewController:self.selfVc];
        
        
    }
}




- (void)reloadCell{
    [self.collectionView reloadData];
    [self.collectionView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@(self.collectionView.collectionViewLayout.collectionViewContentSize.height));
    }];
    if ([self.delegate respondsToSelector:@selector(didChangeHeight:)]) {
        [self.delegate didChangeHeight:self];
    }
    // [self.delegate didChangeCell:self];
}
- (void)ks_photoBrowser:(KSPhotoBrowser *)browser didSelectItem:(KSPhotoItem *)item atIndex:(NSUInteger)index
{
    
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
-(void)textViewDidChange:(UITextView *)textView
{
//    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
//    
//    paragraphStyle.lineSpacing = 3;// 字体的行间距
//
//    NSDictionary *attributes = @{
//                                 NSFontAttributeName:[UIFont systemFontOfSize:16],
//                    NSParagraphStyleAttributeName:paragraphStyle
//                                 };
//    textView.attributedText = [[NSAttributedString alloc] initWithString:textView.text attributes:attributes];
}
@end
